package com.canbot.u05.adapter;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.preside.PresideTask;
import com.canbot.u05.utils.StatusBarUtil;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by lrh on 2017/3/3.
 */

public class HostModeListViewAdapter extends BaseAdapter implements View.OnClickListener {

        private List<PresideTask> dList;

        private Context context;

        private LayoutInflater mInflater;

        private PopupWindow popupWindow;


        public HostModeListViewAdapter(Context context, CopyOnWriteArrayList<PresideTask> dList) {
                super();
                this.context = context;
                mInflater = LayoutInflater.from(context);
                this.dList = dList;
        }

        @Override
        public int getCount() {
                return dList.size();
        }

        @Override
        public PresideTask getItem(int position) {
                return dList.get(position);
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        //然后重写getView
        @Override
        public View getView(final int position, View view, ViewGroup parent) {

                if (view == null) {
                        view = mInflater.inflate(R.layout.hostmode_item, null);
                        Log.e("TAG", "" + view);
                }

                TextView info = (TextView) view.findViewById(R.id.text1);
                info.setText(dList.get(position).getTaskName());

                view.findViewById(R.id.img1).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                showPopupWindow(view, position);
                                Log.e("showPopupWindow", position + "");

                        }
                });


                return view;
        }

        //弹框的显示
        public void showPopupWindow(View view, int position) {

                // 一个自定义的布局，作为显示的内容
                View contentView = LayoutInflater.from(context).inflate(R.layout.hostmode_item_menu, null);
                Log.e("contentView", contentView + "");

                // 设置按钮的点击事件
                Button button1 = (Button) contentView.findViewById(R.id.btn_1);
                Button buttonname = (Button) contentView.findViewById(R.id.btn_name);
                Button buttondelete = (Button) contentView.findViewById(R.id.btn_delete);

                button1.setOnClickListener(this);
                buttonname.setOnClickListener(this);
                buttondelete.setOnClickListener(this);

                popupWindow = new PopupWindow(contentView,
                                              ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, true);


                popupWindow.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Log.i("mengdd", "onTouch : ");
                                return false;
                                // 这里如果返回true的话，touch事件将被拦截
                                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

                        }
                });

                // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                popupWindow.setBackgroundDrawable(new ColorDrawable());

                // 设置好参数之后再show
                //popupWindow.showAsDropDown(view);

                int[] location = new int[2];
                view.getLocationOnScreen(location);
                popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                                StatusBarUtil.hideBottomUIMenu((Activity) context);
                        }
                });
                popupWindow.showAtLocation((View) view.getParent(), Gravity.LEFT | Gravity.TOP, location[0] - (int) context.getResources().getDimension(R.dimen.x240), location[1]);
                if (listner != null) {
                        listner.showPopuwindow(position);
                }
        }

        public interface AdapterOnclickListner {

                void onclick(int id);

                void showPopuwindow(int position);

                void doTask();
        }

        AdapterOnclickListner listner;

        public void setListner(AdapterOnclickListner listner) {
                this.listner = listner;
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        //执行
                        case R.id.btn_1:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.doTask();
                                        }
                                }
                                break;
                        case R.id.btn_name:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.onclick(R.id.btn_name);
                                        }
                                }
                                break;
                        case R.id.btn_delete:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.onclick(R.id.btn_delete);
                                        }
                                }
                                break;

                }
        }
}
